//
//  ChaosNewFeatureController.m
//  ChaosWeiBo
//
//  Created by admin on 16/7/31.
//  Copyright © 2016年 Chaos. All rights reserved.
//

#import "ChaosNewFeatureController.h"
#import "MATabBarViewController.h"

@interface ChaosNewFeatureController ()<UIScrollViewDelegate>
/** pageControl */
@property(nonatomic,strong) UIPageControl *pageControl;

/** scrollView */
@property(nonatomic,strong) UIScrollView *scrollView;
@end

@implementation ChaosNewFeatureController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 1.添加scrollView
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = [UIScreen mainScreen].bounds;
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    
    // 2.给scrollView添加
    int pageCount = 1;
    CGFloat scrollW = scrollView.width;
    for (int i = 0; i < pageCount; i ++) {
        
//        NSString *imageName = [NSString stringWithFormat:@"new_feature_%d",i];
        NSString *imageName = @"1";
        NSString * picPath = [[NSBundle mainBundle] pathForResource:imageName ofType:@"png"];
        NSData * picData = [NSData dataWithContentsOfFile:picPath];
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:picData]];
        imageView.size = scrollView.size;
        imageView.x = i * scrollW;
        imageView.y = 0;
        [scrollView addSubview:imageView];
        if (i == pageCount - 1) { // 给最后一个图片设置内容
            [self setupLastImageView:imageView];
        }
    }
    scrollView.contentSize = CGSizeMake(pageCount * scrollW, 0);
    scrollView.bounces = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.pagingEnabled = YES;
    
    // 3.添加UIPageControl
//    UIPageControl *pageControl = [[UIPageControl alloc] init];
//    pageControl.centerX = scrollW * 0.5;
//    pageControl.centerY = scrollView.height * 0.9;
//    pageControl.numberOfPages = 4;
//    pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
//    pageControl.pageIndicatorTintColor = [UIColor colorWithRed:138 / 255.0 green:138 / 255.0 blue:138 / 255.0 alpha:1.0];
//    [self.view addSubview:pageControl];
//    self.pageControl = pageControl;
    
    // 4.设置scrollView的代理
    scrollView.delegate = self;
}

- (void)setupLastImageView:(UIImageView *)imageView
{
    // 允许用户交互
    imageView.userInteractionEnabled = YES;
    // 1.添加checkbox
//    UIButton *checkBtn = [[UIButton alloc] init];
//    [checkBtn setImage:[UIImage imageNamed:@"new_feature_share_false"] forState:UIControlStateNormal];
//    [checkBtn setImage:[UIImage imageNamed:@"new_feature_share_true"] forState:UIControlStateSelected];
//    [checkBtn setTitle:@"分享给大家" forState:UIControlStateNormal];
//    [checkBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    checkBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
//    checkBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 5);
//    checkBtn.size = CGSizeMake(200, 50);
//    checkBtn.centerX = self.scrollView.centerX;
//    checkBtn.centerY = self.scrollView.height * 0.65;
//    [checkBtn addTarget:self action:@selector(checkBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//    [imageView addSubview:checkBtn];
    // 2.添加开始微博按钮
    UIButton *startWB = [[UIButton alloc] init];
    startWB.width = 100;
    startWB.height = 35;
    startWB.backgroundColor = [UIColor redColor];
//    [startWB setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button"] forState:UIControlStateNormal];
//    [startWB setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
    [startWB setTitle:@"点击进入" forState:UIControlStateNormal];
    startWB.titleLabel.font = [UIFont systemFontOfSize:FONT_SIZE_14];
    startWB.layer.cornerRadius = 5;
    startWB.layer.masksToBounds = YES;
    startWB.centerX = self.scrollView.centerX;
    startWB.centerY = self.scrollView.height * 0.7;
    [startWB addTarget:self action:@selector(startWBClick) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:startWB];
}

- (void)checkBtnClick:(UIButton *)btn
{
    btn.selected = !btn.selected;
}
- (void)startWBClick
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    window.rootViewController = [[MATabBarViewController alloc] init];
}

#pragma mark - scrollView 代理
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat page = scrollView.contentOffset.x / scrollView.width;
    self.pageControl.currentPage = (int)(page + 0.5);
}

@end
